package com.mrd.gtimpframe.service.impl;

import com.mrd.gtimpframe.entity.HgData;
import com.mrd.gtimpframe.repository.HgDataMapper;
import com.mrd.gtimpframe.service.HgDataService;
import com.mrd.redis.JedisClientClusterImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by dusy on 2017/9/21.
 */
@Service
public class HgDataServiceImpl implements HgDataService {
    @Autowired
    private HgDataMapper hgDataMapper;
    @Autowired
    private JedisClientClusterImpl jedisClientCluster;

    @Override
    public int deleteByPrimaryKey(BigDecimal id) {
        return hgDataMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(HgData record) {
        return hgDataMapper.insert(record);
    }

    @Override
    public int insertSelective(HgData record) {
        return hgDataMapper.insertSelective(record);
    }

    @Override
    public HgData selectByPrimaryKey(BigDecimal id) {
        return hgDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(HgData record) {
        return hgDataMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(HgData record) {
        return hgDataMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<HgData> selectAll() {
        return hgDataMapper.selectAll();
    }

    @Override
    public List<HgData> selectByType(String type) {
        return hgDataMapper.selectByType(type);
    }

    @Override
    public String getCodeByName(String name, String type) {
        Map<String, String> hgDataMap = jedisClientCluster.get(type, Map.class);
        if (hgDataMap == null) {
            hgDataMap = selectByType(type).stream().collect(Collectors.toMap(HgData::getName, HgData::getCode));
        }
        return hgDataMap == null ? null : hgDataMap.get(name);
    }
}
